package com.tripcube.ai.config;

import com.alibaba.cloud.ai.memory.jdbc.MysqlChatMemoryRepository;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * 聊天记忆配置类
 * 
 * @author alander
 */
@Configuration
public class ChatMemoryConfig {

    /**
     * 最大保存的消息数量
     */
    private static final int MAX_MESSAGES = 100;

    /**
     * 默认系统提示语
     */
    private static final String DEFAULT_SYSTEM_PROMPT = "你是一个旅游规划智能助手，为用户出现提出建议，请根据用户提问回答！你可以记住之前的对话内容。";

    /**
     * 创建MySQL聊天记录存储仓库
     *
     * @param mysqlJdbcUrl MySQL数据库的JDBC连接URL
     * @param mysqlUsername MySQL数据库的用户名
     * @param mysqlPassword MySQL数据库的密码
     * @param mysqlDriverClassName MySQL数据库的驱动类名
     * @return MysqlChatMemoryRepository实例，用于操作聊天记录的持久化存储
     */
    @Bean
    public MysqlChatMemoryRepository mysqlChatMemoryRepository(
            @Value("${spring.ai.chat.memory.repository.jdbc.mysql.jdbc-url}") String mysqlJdbcUrl,
            @Value("${spring.ai.chat.memory.repository.jdbc.mysql.username}") String mysqlUsername,
            @Value("${spring.ai.chat.memory.repository.jdbc.mysql.password}") String mysqlPassword,
            @Value("${spring.ai.chat.memory.repository.jdbc.mysql.driver-class-name}") String mysqlDriverClassName) {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(mysqlDriverClassName);
        dataSource.setUrl(mysqlJdbcUrl);
        dataSource.setUsername(mysqlUsername);
        dataSource.setPassword(mysqlPassword);

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return MysqlChatMemoryRepository.mysqlBuilder()
                .jdbcTemplate(jdbcTemplate)
                .build();
    }

    /**
     * 创建消息窗口聊天记忆管理器
     *
     * @param mysqlChatMemoryRepository 用于存储聊天记录的MySQL仓库
     * @return MessageWindowChatMemory实例，用于管理聊天记忆
     */
    @Bean
    public MessageWindowChatMemory messageWindowChatMemory(MysqlChatMemoryRepository mysqlChatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(mysqlChatMemoryRepository)
                .maxMessages(MAX_MESSAGES)
                .build();
    }

    /**
     * 创建带聊天记忆功能的ChatClient
     *
     * @param builder ChatClient的构建器
     * @param messageWindowChatMemory 消息窗口聊天记忆实例
     * @return 配置完成的ChatClient实例
     */
    @Bean
    public ChatClient mysqlMemoryChatClient(ChatClient.Builder builder,
                                            MessageWindowChatMemory messageWindowChatMemory) {
        return builder
                .defaultSystem(DEFAULT_SYSTEM_PROMPT)
                .defaultAdvisors(
                        // 聊天记录
                        MessageChatMemoryAdvisor.builder(messageWindowChatMemory)
                                .build(),
                        // 记录日志
                        new SimpleLoggerAdvisor()
                )
                .defaultOptions(
                        // 模型参数
                        DashScopeChatOptions.builder()
                                .withTopP(0.7)
                                .withTemperature(0.8)
                                .build()
                )
                .build();
    }
}

